<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>千锋健康</title>
    <meta name="description" content="千锋健康">
    <meta name="keywords" content="千锋健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>会员管理<small>会员管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item><a href="show.html">首页</a> </el-breadcrumb-item>
            <el-breadcrumb-item>会员管理</el-breadcrumb-item>
            <el-breadcrumb-item>会员管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="编码/名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findOne()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="fileNumber" label="会员编码" align="center"></el-table-column>
                <el-table-column prop="name" label="会员名称" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == '0' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="idcard" label="身份证号" align="center"></el-table-column>
                <el-table-column prop="phoneNumber" label="手机号" align="center"></el-table-column>
                <el-table-column prop="regtime" label="登记时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="memberUpdateInit(scope.row.id)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="memberDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 修改标签弹层 -->
            <div class="update-form">
                <el-dialog :visible.sync="dialogFormEditVisible" title="修改会员">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="会员编号">
                                                <el-input v-model="formData.fileNumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="会员名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="会员性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="身份证号">
                                                <el-input v-model="formData.idcard"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话号码">
                                                <el-input v-model="formData.phoneNumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="初始密码">
                                                <el-input type="password" v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="邮箱">
                                                <el-input v-model="formData.email"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <el-date-picker type="datetime" v-model="formData.birthday" value-format="yyyy-MM-dd"></el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormEditVisible = false">取消</el-button>
                        <el-button type="primary" @click="memberUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增会员" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="会员编号">
                                                <el-input v-model="formData.fileNumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="会员名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="会员性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="身份证号">
                                                <el-input v-model="formData.idcard"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话号码">
                                                <el-input v-model="formData.phoneNumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="初始密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="邮箱">
                                                <el-input v-model="formData.email"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <el-date-picker type="datetime" v-model="formData.birthday" value-format="yyyy-MM-dd"></el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            autoUpload:true,//自动上传
            imageUrl:null,//模型数据，用于上传图片完成后图片预览
            activeName:'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:3,
                total:0,
                queryString:null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData:[],//添加表单窗口中检查组列表数据
            checkgroupIds:[],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormEditVisible: false//控制添加窗口显示/隐藏
        },
        mounted(){
            this.findPage();
        },
        methods: {
            //修改前重置信息
            memberUpdateInit(id){
                //重置表单数据
                this.resetForm();
                this.dialogFormEditVisible = true;
                axios({
                    url:"http://localhost:8080/member/memberUpdateInit",
                    method:"get",
                    params: {id:id},
                    headers: {
                        token: this.token()
                    }
                }).then((res) => {
                    if(res.data.code == 200){
                        //为formData赋值，基于VUE数据绑定将数据展示到表格中
                        this.formData = res.data.data;
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },
            //修改后回显信息
            memberUpdate(){
                //发送ajax请求，提交表单，包括两部分：套餐基本信息、关联的检查组id数组
                axios({
                    url:"http://localhost:8080/member",
                    method:"put",
                    data: this.formData,
                    contentType: "application/json",
                    headers: {
                        token: this.token()
                    }
                }).then((res) => {
                    if(res.data.code == 200){
                        //执行成功，提示信息
                        this.$message({
                            type:'success',
                            message:res.data.message
                        });
                        //关闭新增窗口
                        this.dialogFormEditVisible = false;
                        //分页查询
                        this.findPage();
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },
            //查询单个
            findOne(){
                this.formData = {}

                if(this.pagination.queryString == null || this.pagination.queryString == ""){
                    this.findPage()
                }else {
                    axios({
                        url:"http://localhost:8080/member/"+this.pagination.queryString,
                        method:"get",
                        headers:{
                            token: this.token()
                        }
                    }).then(resp=>{
                        let result = resp.data;
                        let code = result.code
                        if (code == 200){
                            this.dataList = result.data;
                            this.pagination.total = 1;
                        }else {
                            this.$message.error(resp.data.message);
                            this.findPage()
                        }
                    });
                }
            },
            //删除成员
            memberDelete(id){
                let a = confirm("确定删除吗")
                if(a){
                    axios({
                        url:"http://localhost:8080/member/" +id,
                        method:"delete",
                        headers: {
                            token: this.token()
                        }
                    }).then((res) => {
                        if(res.data.code == 200){
                            //执行成功，提示信息
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            //分页查询
                            this.findPage();
                        }else{
                            this.$message.error(res.data.message);
                        }

                    });
                }

            },
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.$message({
                    type:response.flag?"success":"error",
                    message:response.message
                });
                if(response.flag){
                    //预览刚刚上传的文件
                    this.imageUrl = "http://puco9aur6.bkt.clouddn.com/"+response.data;
                    this.formData.img = response.data;//将文件名称赋值给模型数据，用于表单提交
                }
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //添加
            handleAdd () {
                //发送ajax请求，提交表单，包括两部分：套餐基本信息、关联的检查组id数组
                axios({
                    url:"http://localhost:8080/member",
                    method:"post",
                    data: this.formData,
                    contentType: "application/json",
                    headers: {
                        token: this.token()
                    }
                }).then((res) => {
                    if(res.data.code == 200){
                        //执行成功，提示信息
                        this.$message({
                            type:'success',
                            message:res.data.message
                        });
                        //关闭新增窗口
                        this.dialogFormVisible = false;
                        //分页查询
                        this.findPage();
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },
            //分页查询
            findPage() {
                let token = localStorage.getItem("security_token");
                //发送异步请求，加载当前页的数据 (当前页+每页多少条+查询条件)，只需要将模型的pagination携带到后台即可
                axios({

                    url:"http://localhost:8080/member",
                    method:"get",
                    params: {
                        currentPage: this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        total:this.pagination.total
                    },
                    headers:{
                        token:token
                    }
                }).then(resp=>{
                    let result = resp.data;
                    let code = result.code
                    if (code == 200){
                        let result = resp.data;
                        this.dataList = result.data.records;
                        //将数据库的总记录数赋值到分页条的total属性上
                        this.pagination.total = result.data.total;
                    }else {
                        location.href = "show.html"
                    }

                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.activeName = 'first';
                this.checkgroupIds = [];
                this.imageUrl = null;
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单数据
                this.resetForm();
                this.dialogFormVisible = true;
                axios({
                    url:"http://localhost:8080/member/insertReset",
                    method:"get",
                    headers: {
                        token: this.token()
                    }
                }).then((res) => {
                    if(res.data.code == 200){
                        //为formData赋值，基于VUE数据绑定将数据展示到表格中
                        this.formData = res.data.data;
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },
            //获取token
            token(){
                let token1=localStorage.getItem("security_token");
                return token1
            }
        }
    })
</script>
</html>
